package com.liaoyifan.core.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@SuppressWarnings("unused")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FileName {

    public static final char EXTENSION_SEPARATOR = '.';
    private static final char UNIX_SEPARATOR = '/';
    private static final char WINDOWS_SEPARATOR = '\\';
    private static final char UUID_SEPARATOR = '_';

    public static String getBaseName(String fileName) {
        return removeExtension(getName(fileName));
    }

    public static String getName(String fileName) {
        if (fileName == null) {
            return null;
        }
        int index = indexOfLastSeparator(fileName);
        return fileName.substring(index + 1);
    }

    public static String getExtension(String fileName) {
        if (fileName == null) {
            return null;
        }
        int index = indexOfExtension(fileName);
        if (index == -1) {
            return "";
        } else {
            return fileName.substring(index + 1);
        }
    }

    /**
     * 为文件名添加UUID
     *
     * @param fileName 原始文件名
     * @return 带UUID的文件名
     */
    public static String addUUID(String fileName) {
        return getBaseName(fileName)
                + UUID_SEPARATOR
                + UID.get()
                + EXTENSION_SEPARATOR
                + getExtension(fileName);
    }

    /**
     * 从文件名中去除UUID
     *
     * @param fileName 带UUID的文件名
     * @return 原始文件名
     */
    public static String removeUUID(String fileName) {
        if (fileName == null || fileName.isBlank()) {
            return fileName;
        }
        int uuidSeparatorIndex = fileName.lastIndexOf(UUID_SEPARATOR);
        if (uuidSeparatorIndex == -1) {
            return fileName;
        }
        // 检查UUID部分是否存在且长度正确
        String uuidCandidate = fileName.substring(uuidSeparatorIndex + 1);
        int dotIndex = uuidCandidate.lastIndexOf(EXTENSION_SEPARATOR);
        // UUID长度为32位(不含横杠)
        if (dotIndex != -1) {
            String uuidPart = uuidCandidate.substring(0, dotIndex);
            if (uuidPart.length() == 32) {
                return fileName.substring(0, uuidSeparatorIndex)
                        + fileName.substring(uuidSeparatorIndex + 1 + 32);
            }
        } else if (uuidCandidate.length() == 32) {
            return fileName.substring(0, uuidSeparatorIndex);
        }
        // 如果不是有效的UUID格式，返回原始文件名
        return fileName;
    }

    /** 私有辅助方法 * */
    private static String removeExtension(String fileName) {
        if (fileName == null) {
            return null;
        }
        int index = indexOfExtension(fileName);
        if (index == -1) {
            return fileName;
        } else {
            return fileName.substring(0, index);
        }
    }

    private static int indexOfExtension(String fileName) {
        if (fileName == null) {
            return -1;
        }
        int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
        int lastSeparator = indexOfLastSeparator(fileName);
        return (lastSeparator > extensionPos ? -1 : extensionPos);
    }

    private static int indexOfLastSeparator(String fileName) {
        if (fileName == null) {
            return -1;
        }
        int lastUnixPos = fileName.lastIndexOf(UNIX_SEPARATOR);
        int lastWindowsPos = fileName.lastIndexOf(WINDOWS_SEPARATOR);
        return Math.max(lastUnixPos, lastWindowsPos);
    }
}
